/**
 * Created by wengms on 2016/11/20.
 */
define([angular], function () {
    return function($scope,GillionMsg,Resource,$dataSourceManager){
        $scope.package={};
        $scope.removeShow=false;
        var dataDictRes = Resource("/grule-manager/data_dict/:id",{
            "id":"@id"
        });
        var dataDictDetailRes = Resource("/grule-manager/data_dict_detail/:id",{
            "id":"@id"
        });
        $scope.actions = {
            deletePackage:function(id){
                GillionMsg.confirm('提示信息', '是否确定删除？',
                    function (r) {
                        if (r) {
                            dataDictRes.delete({"id":id},function(response){
                                if (response.success){
                                    GillionMsg.alert("提示", "字典大类删除成功");
                                    $scope.actions.refreshTable();
                                }else{
                                    GillionMsg.alert("提示","字典大类删除失败");
                                    $scope.actions.refreshTable();
                                }
                            });
                        }
                    })
            },
            deletePackage2:function(id){
                GillionMsg.confirm('提示信息', '是否确定删除？',
                    function (r) {
                        if (r) {
                            dataDictDetailRes.delete({"id":id},function(response){
                                if (response.success){
                                    GillionMsg.alert("提示", "字典数据删除成功");
                                    $dataSourceManager.dataSources['dataDictDetailSource'].doRequestData();
                                }else{
                                    GillionMsg.alert("提示","字典数据删除失败");
                                    $dataSourceManager.dataSources['dataDictDetailSource'].doRequestData();
                                }
                            });
                        }
                    })
            },
            openEditView:function(dataDict){
                var title = !dataDict?"新建大类":"大类编辑";
                GillionMsg.showUrl({
                    title:title,
                    data:dataDict,
                    url: '/html2/system/dataDict/data-dict',
                    width:400,
                    height:360,
                    onClose:function(data){
                        //刷新表格内容
                        $scope.actions.refreshTable();
                    }
                });
            },
            search:function(){
                var params={};
                params.dataName =$scope.dataDict.dataName;
                $dataSourceManager.dataSources['dataDictSource'].params=params;
                $dataSourceManager.dataSources['dataDictSource'].doRequestData();
            },
            search2:function(){
                if($scope.dataId==undefined){
                    GillionMsg.alert("提示","请先选择字典大类！");
                    return;
                }
                var params={};
                params.dataName =$scope.dataDictDetail.dataName;
                params.dataId =$scope.dataId;
                $dataSourceManager.dataSources['dataDictDetailSource'].params=params;
                $dataSourceManager.dataSources['dataDictDetailSource'].doRequestData();
            },
            searchChildren:function(id){
                $scope.dataId = id;
                var param={};
                param.dataId =id;
                $dataSourceManager.dataSources['dataDictDetailSource'].params=param;
                $dataSourceManager.dataSources['dataDictDetailSource'].doRequestData();
            },
            refreshTable:function(){
                $dataSourceManager.dataSources['dataDictSource'].doRequestData();
            },
            addChild:function(id,mode){
                GillionMsg.showUrl({
                    title:"新增数据字典",
                    data:{"dataId":id},
                    url: '/html2/system/dataDict/data-dict-detail',
                    width:400,
                    height:400,
                    onClose:function(data){
                        //刷新表格内容
                        $dataSourceManager.dataSources['dataDictDetailSource'].doRequestData();
                    }
                });
            },
            openEditView2:function(row){
                GillionMsg.showUrl({
                    title:"编辑数据字典",
                    data:row,
                    url: '/html2/system/dataDict/data-dict-detail',
                    width:400,
                    height:400,
                    onClose:function(data){
                        //刷新表格内容
                        $dataSourceManager.dataSources['dataDictDetailSource'].doRequestData();
                    }
                });
            }
        }
        $scope.myKeyup = function(e){
            var keycode = window.event?e.keyCode:e.which;
            if(keycode==13){
                $scope.actions.search();
            }
        };
        $scope.$watch('package.name',function(newValue,oldValue){
            if(newValue==""){
                $scope.removeShow=false;
                $scope.actions.search();
            }else if(newValue){
                $scope.removeShow=true;
            }
        });
        $scope.deleteInput=function(){
            $scope.package={name:""};
        }
    };
});